package com.kimu.user.entity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Date;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.RequiredArgsConstructor;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

@Data
@RequiredArgsConstructor
@TableName("`users`")
public class User implements UserDetails {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -2511760516000888985L;
	
	@TableId(value = "id", type=IdType.ASSIGN_ID)
	private Long id;
	
	private String username;
	
	private String password;
	
	private boolean enabled = true;
	
	@TableField(value = "first_name")
	private String firstName;
	
	@TableField(value = "last_name")
	private String lastName;
	
	private Short gender;
	
	private String email;
	
	private String icon;
	
	private String location;
	
	private Date birthday;
	
	@TableField(value = "joined_date")
	private Date joinedDate;
	
	@TableLogic
	@TableField(value = "is_deleted")
	private Boolean isDeleted;
	
	@TableField(exist = false)
	private List<Authority> authorities = new ArrayList<>();

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		// TODO Auto-generated method stub
		List<GrantedAuthority> auths = new LinkedList<>();
		for (Authority authority : this.authorities) {
			auths.add(new SimpleGrantedAuthority(authority.getAuthority()));
		}
		return auths;
	}

	@Override
	public String getPassword() {
		// TODO Auto-generated method stub
		return this.password;
	}

	@Override
	public String getUsername() {
		// TODO Auto-generated method stub
		return this.username;
	}

	
}
